#include "login.h"
#include "ui_login.h"
#include "QDebug"
#include "QMessageBox"
#include "socket/csocketcommunication.h"
#include "socket/protocol/CarLogin.h"
#include "globalParameterForTaxi.h"

#include "qbitmap.h"

extern const int CHARACTERLENGTH = 10;
extern DRIVER_NAME g_driverInfo;

interface::CLoginInterface::CLoginInterface(IInterface *parent)
    :IInterface(parent),
    m_pUi(new Ui::Login)
{
    m_pUi->setupUi(this);

    // setup the background
    this->UiSetting(m_pUi);

    m_pCarLogin = NULL;

    m_pUi->lineEditPassword->setEchoMode(QLineEdit::Password);

    // connect between buttons and actions
    connect(m_pUi->buttonExit, SIGNAL(clicked()), this, SLOT(close()) );
    connect(m_pUi->buttonOK, SIGNAL(clicked()), this, SLOT(login()) );

    //connect the signals from socket
    connect(CSocketCommunication::GetInstance(), SIGNAL(canRead()), this, SLOT(readResult()) );
    connect(CSocketCommunication::GetInstance(), SIGNAL(socketError()), this, SLOT(socketError()) );
}

interface::CLoginInterface::~CLoginInterface()
{
    delete m_pUi;

    if(m_pCarLogin != NULL)
    {
        delete m_pCarLogin;
        m_pCarLogin = NULL;
    }
}

void interface::CLoginInterface::login()
{
    qDebug() << "login request has been sent out";

    QString userName = m_pUi->lineEditUsername->text();
    QString password = m_pUi->lineEditPassword->text();

    // store the userName to the global variable
    g_driverInfo.name = userName;

    if(userName.isEmpty() || password.isEmpty())
    {
        QMessageBox::information(this, tr("warning"), tr("username and password can not be NULL"));
        return;
    }

    // length() will not calculate the length of '\0'
    if(userName.length() > CHARACTERLENGTH - 1 || password.length() > CHARACTERLENGTH - 1)
    {
        QMessageBox::information(this, tr("warning"), tr("Username and password are too long"));
        return;
    }

    if (m_pCarLogin != NULL)
    {
        delete m_pCarLogin;
        m_pCarLogin = NULL;
    }
    m_pCarLogin = new protocal::CarLogin;

    // TODO: all these completion should be wrapped in a function.
    int i = 0;
    // we need to complete the string to length 10
    while(userName.length() + i < CHARACTERLENGTH - 1)
    {
        userName.insert(userName.length(), QChar('\t'));
        i++;
    }
    memcpy(m_pCarLogin->user_name, userName.toStdString().c_str(), userName.toStdString().length() + 1);

    i = 0;
    // we need to complete the string to length 10
    while(password.length() + i < CHARACTERLENGTH - 1)
    {
        password.insert(password.length(), QChar('\t'));
        i++;
    }
    memcpy(m_pCarLogin->password, password.toStdString().c_str(), password.toStdString().length() + 1);

    // send out the request
    CSocketCommunication::GetInstance()->sendRequest(protocal::CAR_LOGIN, (void*)m_pCarLogin, sizeof(protocal::CarLogin));
}

void interface::CLoginInterface::readResult()
{
    qDebug() << "login verification has recieved. You can now read the result";

    QString ans = CSocketCommunication::GetInstance()->getResult(QString("answer"));
    if (ans.toInt() == HAS_PASSED)
    {
        // the user has been verified
        qDebug() << "the user has been verified\n";

        m_param.carId = CSocketCommunication::GetInstance()->getResult(QString("driver_id"));

        // store into the global variable
        g_driverInfo.car_id = m_param.carId;
        // g_driverInfo.name is stored the first time we enter the user name
        m_param.name = g_driverInfo.name;

        // change the interface to Service Choose
        emit changeInterfaces(LOGIN, SERVICE_CHOOSE, (void*)&m_param);
    }
    else
    {
        // the user has been denied
        qDebug() << "the user has been denied\n";

        QMessageBox::information(this, tr("warning"), \
                                 CSocketCommunication::GetInstance()->getErrorString().append(tr("\nplease try again!")));
    }
}

void interface::CLoginInterface::socketError()
{
    QMessageBox::information(this, tr("warning in login"), \
                             CSocketCommunication::GetInstance()->getErrorString().append(tr("\nplease try again!")));
}

void interface::CLoginInterface::UiSetting(Ui::Login *ui)
{
    //setup the background
    IInterface::SetupBackground(QString("images\\background.jpg"));

    //setup the username lable
    IInterface::SetupLabel(ui->LabelUserName, QString("images\\login\\username.png"));

    //setup the password lable
    IInterface::SetupLabel(ui->LabelPassword, QString("images\\login\\password.png"));

    //setup the login button
    IInterface::SetupBtn(ui->buttonOK, QString("images\\login\\login.png"));

    //setup the exit button
    IInterface::SetupBtn(ui->buttonExit, QString("images\\login\\exit.png"));
}
